<?php

/**
 * Description of Geocoding
 * Resources: https://developers.google.com/maps/documentation/geocoding/
 * @author Samsung
 */
class Geocoder {
    
    const 
        STREET_ADDRESS = 'street_address',
        STATE = 'administrative_area_level_1',
        ADDRESS2 = 'administrative_area_level_2',
        ADDRESS3 = 'administrative_area_level_3',
        LOCALITY = 'locality', // city or town
        SUBLOCALITY = 'sublocality',
        POSTAL_CODE = 'postal_code',
        COUNTRY = 'country',
        LATITUDE = 'latitude',
        LONGITUDE = 'longitude',
        FORMATTED_ADDRESS = 'formatted_address'
        ;
    
    public $address;
    public $latlng; // Example: 40.714224,-73.961452
    public $bounds;
    public $language;
    public $region;
    public $components; // component:value pairs. Example: component1:value1|component2:value2|...
    public $sensor=true;
    public $content;
    public $data;
    public $hasResult=false;
    public $resultCount=0;
    public $addressComponents;
    
    private $_format;
    private $_api='http://maps.googleapis.com/maps/api/geocode/';
    
    
    public function __construct($address=null, $format='json'){
        $this->address=$address;
        $this->_format=strtolower($format);
    }
        
    public function getResponse(){
        echo $url = $this->_api.$this->_format.'?'.implode('&', array_filter(array(
            empty($this->address)?'':'address='.urlencode($this->address),
            empty($this->latlng)?'':$this->latlng,
            empty($this->bounds)?'':'bounds='.$this->bounds,
            empty($this->language)?'':'language='.$this->language,
            empty($this->region)?'':'region='.$this->region,
            empty($this->components)?'':'components='.$this->components,
            $this->sensor? 'sensor=true':'sensor=false',
        )));
        
        if (1) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);       
            $this->content=curl_exec($ch);
            curl_close($ch);
        } else {
            $this->content = @file_get_contents($url);
        }
        
        if(!empty($this->content)){
            $this->data=json_decode($this->content, true);
            if(in_array($this->data['status'], array('OK'))){
                $this->hasResult=true;
                $this->resultCount=count($this->data['status']);
                
                foreach($this->data['results'] as $key=>$address){
                    foreach($address['address_components'] as $value){
                        $this->addressComponents[$key][$value['types'][0]]=$value['long_name'];
                    }
                    
                    $this->addressComponents[$key]['formatted_address']=$address['formatted_address'];
                    $this->addressComponents[$key]['latitude']=$address['geometry']['location']['lat'];
                    $this->addressComponents[$key]['longitude']=$address['geometry']['location']['lng'];
                }
            }
        }
    }
        
    public function get($type=null, $index=0){
        $type=strtolower($type);
        return isset($this->addressComponents[$index][$type])? $this->addressComponents[$index][$type]:null;
    }
    
}